﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using App.Store;
using App.Contract;
using App.SysOption;

namespace App
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
            ToolStripManager.Renderer = new Office2007Renderer();
            InitUI();
            //
        }

        //protected override CreateParams CreateParams
        //{
        //    get
        //    {
        //        CreateParams cp = base.CreateParams;
        //        cp.ExStyle |= 0x02000000;
        //        return cp;
        //    }
        //}

        private void InitUI()
        {
            timer1.Enabled = true;
            if (Startup.GetApp().CurrentUser != null)
            {
                lb1.Text = "操作人：" + Startup.GetApp().CurrentUser.name;
                lb2.Text = "部门：" + Startup.GetApp().CurrentUser.department;
                switch (Convert.ToInt32(Startup.GetApp().CurrentUser.sPosition))
                {
                    case 11://经理
                        this.lb3.Text = "职位：经理";
                        break;
                    case 21://组长
                        this.lb3.Text = "职位：组长";
                        break;
                    case 31://业务员
                        this.lb3.Text = "职位：业务员";
                        break;
                    case 32://助理
                        this.lb3.Text = "职位：助理";
                        break;
                    default:
                        break;
                }
            }
        }


        private void ShowForm(Form frm)
        {
            frm.MdiParent = this;
            frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
            frm.ControlBox = false;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }

        private void frmMain_MdiChildActivate(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild == null)
                tabForms.Visible = false; // If no any child form, hide tabControl
            else
            {
                //this.ActiveMdiChild.WindowState = FormWindowState.Maximized; // Child form always maximized

                // If child form is new and no has tabPage, create new tabPage
                if (this.ActiveMdiChild.Tag == null)
                {
                    // Add a tabPage to tabControl with child form caption
                    TabStripItem tp = new TabStripItem(this.ActiveMdiChild.Text,null);
                    //tp.CanClose = false;
                    tp.Tag = this.ActiveMdiChild;

                    this.ActiveMdiChild.Tag = tp;
                    this.ActiveMdiChild.FormClosed += new FormClosedEventHandler(ActiveMdiChild_FormClosed);

                    tabForms.AddTab(tp,true);
                }

                if (!tabForms.Visible) tabForms.Visible = true;
            }
        }

        private void ActiveMdiChild_FormClosed(object sender, FormClosedEventArgs e)
        {
            ((sender as Form).Tag as TabStripItem).Dispose();
        }


        private void tabForms_SelectedItemChanged(TabStripItemMouseEventArgs e)
        {
            if ((tabForms.SelectedTab != null) && (tabForms.SelectedTab.Tag != null))
            {
                Form frm = tabForms.SelectedTab.Tag as Form;
                if(frm != null)
                {
                    frm.BringToFront();
                }
            }
               
        }

        private void tabForms_TabStripItemClosing(TabStripItemClosingEventArgs e)
        {
            Form frm = e.Item.Tag as Form;
            if (frm != null) frm.Close();
        }

        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Startup.GetApp().Close();
        }

        private void SystemSetup_Click(object sender, EventArgs e)
        {
            if (sender == msLoginNew)
            {
                System.Diagnostics.Process.Start(Application.ExecutablePath, "clone");
                Startup.GetApp().Close();
            }
            else if (sender == msModifyPwd)
            {
                
            }
            else if (sender == msSystem)
            {
                
            }
            else if (sender == msRole)
            {
               
            }
            else if (sender == msUser)
            {
                frmUserManage frmuser = new frmUserManage();
                ShowForm(frmuser);
            }
            //else if (sender == msBaseInfo)
            //{
            //    frmBaseInfo frmBI = new frmBaseInfo();
            //    ShowForm(frmBI);
            //}
            else if (sender == msExit)
            {
                if (Msg.ShowInfo("您确信要关闭该管理系统吗？", MsgType.Help) == System.Windows.Forms.DialogResult.Yes)
                {
                    Startup.GetApp().Close();
                }
            }
        }

        private void StoreManage_Click(object sender, EventArgs e)
        {
            if (sender == msSampleAdd)
            {
                frmSample sample = new frmSample();
                ShowForm(sample);
            }
            else if (sender == msSamplePre)
            {
                frmSamplePre samplepre = new frmSamplePre();
                ShowForm(samplepre);
            }
            else if (sender == msSampleStore)
            {
                frmSampleStore frmsamsto = new frmSampleStore();
                ShowForm(frmsamsto);
            }
            else if (sender == msBuyerReturn)
            {
                frmSampleYWYReturn frmsamreturn = new frmSampleYWYReturn();
                ShowForm(frmsamreturn);
            }
            else if (sender == msSrReturn)
            {
                frmSampleYPJReturn frmsrReturn = new frmSampleYPJReturn();
                ShowForm(frmsrReturn);
            }
            else if (sender == msLend)
            {
                frmSampleLend frmLend = new frmSampleLend();
                ShowForm(frmLend);
            }
            else if (sender == msSend)
            {
                frmSampleSend frmSend = new frmSampleSend();
                ShowForm(frmSend);
            }
            else if (sender == msSrManage)
            {
                frmSampleManagement frmsrManage = new frmSampleManagement();
                ShowForm(frmsrManage);
            }
            else if (sender == msStoreManage)
            {
                frmStoreManagement frmstoreManage = new frmStoreManagement();
                ShowForm(frmstoreManage);
            }
            else if (sender == msSelectOption)
            {
                frmStateSelect frmSelect = new frmStateSelect();
                ShowForm(frmSelect);
            }
        }

        private void ContractManage_Click(object sender, EventArgs e)
        {
            if (sender == msOutContract)
            {
                frmOutContract frmout = new frmOutContract();
                ShowForm(frmout);
            }
            else if (sender == msInContract)
            {
                frmInContract frmin = new frmInContract();
                ShowForm(frmin);
            }
            else if (sender == msOrderTable)
            {
                frmorder frmord = new frmorder();
                ShowForm(frmord);
            }
        }

        private void CustAndProvide_Click(object sender, EventArgs e)
        {
            if (sender == msCustMsg)
            {
                frmCust frmcu = new frmCust();
                ShowForm(frmcu);
            }
            else if (sender == msProviderMsg)
            {
                frmSupplier frmsu = new frmSupplier();
                ShowForm(frmsu);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            mDatetime.Text = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();
        }
    }
}
